<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="影视ID" prop="movieId">
        <el-input
          v-model="queryParams.movieId"
          placeholder="请输入影视ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入资源标题"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="播放来源" prop="playFrom">
        <el-select v-model="queryParams.playFrom" placeholder="请选择播放来源" clearable>
          <el-option label="优酷" value="youku" />
          <el-option label="腾讯视频" value="qq" />
          <el-option label="爱奇艺" value="iqiyi" />
          <el-option label="bilibili" value="bilibili" />
          <el-option label="芒果TV" value="mgtv" />
          <el-option label="其他" value="other" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['movie:resource:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['movie:resource:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['movie:resource:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['movie:resource:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="resourceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="资源ID" align="center" prop="resourceId" width="80" />
      <el-table-column label="影视ID" align="center" prop="movieId" width="80" />
      <el-table-column label="资源标题" align="center" prop="title" :show-overflow-tooltip="true" min-width="120" />
      <el-table-column label="播放来源" align="center" prop="playFrom" width="120">
        <template #default="scope">
          <el-tag v-if="scope.row.playFrom === 'youku'" type="primary">优酷</el-tag>
          <el-tag v-else-if="scope.row.playFrom === 'qq'" type="success">腾讯视频</el-tag>
          <el-tag v-else-if="scope.row.playFrom === 'iqiyi'" type="warning">爱奇艺</el-tag>
          <el-tag v-else-if="scope.row.playFrom === 'bilibili'" type="danger">bilibili</el-tag>
          <el-tag v-else-if="scope.row.playFrom === 'mgtv'" type="info">芒果TV</el-tag>
          <el-tag v-else-if="scope.row.playFrom">{{ scope.row.playFrom }}</el-tag>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="播放服务器" align="center" prop="playServer" :show-overflow-tooltip="true" width="150" />
      <el-table-column label="播放备注" align="center" prop="playNote" :show-overflow-tooltip="true" width="150" />
      <el-table-column label="资源标识" align="center" prop="resources" :show-overflow-tooltip="true" width="100" />
      <el-table-column label="资源地址" align="center" prop="url" :show-overflow-tooltip="true" min-width="150">
        <template #default="scope">
          <el-link :href="scope.row.url" type="primary" target="_blank" v-if="scope.row.url">
            {{ scope.row.url.substring(0, 50) }}{{ scope.row.url.length > 50 ? '...' : '' }}
          </el-link>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150" fixed="right">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['movie:resource:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['movie:resource:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改视频资源对话框 -->
    <el-dialog :title="title" v-model="open" width="800px" append-to-body>
      <el-form ref="resourceRef" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="影视ID" prop="movieId">
              <el-input-number v-model="form.movieId" :min="1" placeholder="请输入影视ID" style="width: 100%;" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="资源标识" prop="resources">
              <el-input v-model="form.resources" placeholder="请输入资源标识" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="资源标题" prop="title">
              <el-input v-model="form.title" placeholder="请输入资源标题" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="播放来源" prop="playFrom">
              <el-select v-model="form.playFrom" placeholder="请选择播放来源" style="width: 100%;">
                <el-option label="优酷" value="youku" />
                <el-option label="腾讯视频" value="qq" />
                <el-option label="爱奇艺" value="iqiyi" />
                <el-option label="bilibili" value="bilibili" />
                <el-option label="芒果TV" value="mgtv" />
                <el-option label="其他" value="other" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="播放服务器" prop="playServer">
              <el-input v-model="form.playServer" placeholder="请输入播放服务器地址" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="播放备注" prop="playNote">
              <el-input v-model="form.playNote" placeholder="请输入播放备注信息" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="资源地址" prop="url">
              <el-input v-model="form.url" type="textarea" :rows="3" placeholder="请输入资源URL地址" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入备注信息" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="VideoResource">
import { listVideoResource, getVideoResource, delVideoResource, addVideoResource, updateVideoResource } from "@/api/movie/videoResource";
import { getCurrentInstance, ref, reactive, toRefs } from 'vue';

const { proxy } = getCurrentInstance();

const resourceList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

// 从URL参数获取movieId
const route = proxy.$route;
const movieIdFromRoute = route.query?.movieId;

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    movieId: movieIdFromRoute || null,
    title: null,
    playFrom: null
  },
  rules: {
    movieId: [
      { required: true, message: "影视ID不能为空", trigger: "blur" }
    ],
    title: [
      { required: true, message: "资源标题不能为空", trigger: "blur" }
    ],
    url: [
      { required: true, message: "资源地址不能为空", trigger: "blur" }
    ]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询视频资源列表 */
function getList() {
  loading.value = true;
  listVideoResource(queryParams.value).then(response => {
    resourceList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    resourceId: null,
    movieId: movieIdFromRoute || null,
    title: null,
    resources: null,
    playFrom: null,
    playServer: null,
    playNote: null,
    url: null,
    remark: null
  };
  proxy.resetForm("resourceRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  queryParams.value.movieId = movieIdFromRoute || null;
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.resourceId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加视频资源";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _resourceId = row.resourceId || ids.value;
  getVideoResource(_resourceId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改视频资源";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["resourceRef"].validate(valid => {
    if (valid) {
      if (form.value.resourceId != null) {
        updateVideoResource(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addVideoResource(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _resourceIds = row.resourceId || ids.value;
  proxy.$modal.confirm('是否确认删除视频资源编号为"' + _resourceIds + '"的数据项？').then(function() {
    return delVideoResource(_resourceIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('movie/resource/export', {
    ...queryParams.value
  }, `video_resource_${new Date().getTime()}.xlsx`)
}

getList();
</script>

<style scoped>
</style>
